package com.dylon.seapiweb.controller;

import com.dylon.common.base.AjaxResult;
import com.dylon.common.config.Global;
import com.dylon.common.utils.FileUploadUtils;
import com.dylon.common.utils.ShiroUtils;
import com.dylon.common.utils.StringUtils;
import com.dylon.system.domain.SysUsers;
import com.dylon.system.service.ISysDictDataService;
import com.dylon.system.service.ISysUserService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 业务处理
 */
@Controller
@RequestMapping("/system/user/profile")
@Log4j2
public class SysProfileController extends BaseController {

    private String prefix = "system/user/profile";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 个人信息
     */
    @GetMapping()
    public String profile(ModelMap mmap){
        SysUsers user = getSysUser();
        user.setSex(dictDataService.selectDictLabel("sys_user_sex", user.getSex()));
        mmap.put("user", user);
        mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId()));
        return prefix + "/profile";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password) {
        SysUsers user = getSysUser();
        if (ShiroUtils.passWordMatches(user, password)) {
            return true;
        }
        return false;
    }

    @GetMapping("/resetPwd")
    public String resetPwd(ModelMap mmap) {
        SysUsers user = getSysUser();
        mmap.put("user", userService.selectUserById(user.getUserId()));
        return prefix + "/resetPwd";
    }

    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(String oldPassword, String newPassword) {
        SysUsers user = getSysUser();
        if (StringUtils.isNotEmpty(newPassword) && ShiroUtils.passWordMatches(user, oldPassword)) {
            user.setSalt(ShiroUtils.randomSalt());
            user.setPassWord(DigestUtils.md5Hex(user.getPassWord()));
            if (userService.resetUserPwd(user) > 0) {
                setSysUser(userService.selectUserById(user.getUserId()));
                return success();
            }
            return error();
        } else {
            return error("修改密码失败，旧密码错误");
        }
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit")
    public String edit(ModelMap mmap) {
        SysUsers user = getSysUser();
        mmap.put("user", userService.selectUserById(user.getUserId()));
        return prefix + "/edit";
    }

    /**
     * 修改头像
     */
    @GetMapping("/photo")
    public String avatar(ModelMap mmap) {
        SysUsers user = getSysUser();
        mmap.put("user", userService.selectUserById(user.getUserId()));
        return prefix + "/photo";
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(SysUsers user) {
        SysUsers currentUser = getSysUser();
        currentUser.setUserName(user.getUserName());
        currentUser.setEmail(user.getEmail());
        currentUser.setPhone(user.getPhone());
        currentUser.setSex(user.getSex());
        if (userService.updateUserInfo(currentUser) > 0) {
            setSysUser(userService.selectUserById(currentUser.getUserId()));
            return success();
        }
        return error();
    }

    /**
     * 保存头像
     */
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file) {
        SysUsers currentUser = getSysUser();
        try {
            if (!file.isEmpty()) {
                String photo = FileUploadUtils.upload(Global.getAvatarPath(), file);
                currentUser.setPhoto(photo);
                if (userService.updateUserInfo(currentUser) > 0) {
                    setSysUser(userService.selectUserById(currentUser.getUserId()));
                    return success();
                }
            }
            return error();
        } catch (Exception e) {
            log.error("修改头像失败！", e);
            return error(e.getMessage());
        }
    }
}
